<?php

use swap\visitor;
use swap\lazy_checker;
use swap\check_failed;

class user_controller extends \swap\controller {

    public static $layout = 'my';

    public static function before_run() {
        self::role('user', 'redirect_to', 'my-site/login');
    }

    public static function index_action() {
        $index = user_service::get_index_data();
        self::set('index', $index);
        self::set('resume_count', count($index['resumes']));
        
        self::show_page();
    }

    public static function edit_action() {
        self::show_page();
    }

    public static function do_edit_action() {
        // 拦截
        self::method('post');       
        self::csrf('user');

        try {
            // 校验
            $checker = new lazy_checker(p());
            $checker->check_model_rules('user_info', 'edit');
            $post = $checker->get_all();
            if (p_has('email')) {
                $rule = user_info_model::email_rule();
                $checker->check('email', $rule['email']);
            }
            // 执行
            $user_id = visitor::get_role_id('user');
            $data = array(
                'user_id' => $user_id,
                'username' => $post['username'],
                'gender' => $post['gender'],
                'birthday' => strtotime($post['birthday']),
                'experience' => strtotime($post['experience']),
                'marital' => $post['marital'],
                'id_type' => $post['id_type'],
                'id_number' => $post['id_number'],
                'political_status' => $post['political_status'],
                'resident_province' => $post['resident_province'],
                'resident_city' => $post['resident_city'],
                'live_province' => $post['live_province'],
                'live_city' => $post['live_city'],
                'address' => $post['address'],
                'postal_code' => $post['postal_code'],
                'contact_type' => $post['contact_type'],
                'contact_number' => $post['contact_num'],
                'homepage' => $post['homepage'],
                'email' => p_has('email') ? $post['email'] : visitor::get_role_var('user', 'email'),
            );
            if ($post['user_info_id'] !== 0) {
                user_info_model::set_by_id($data, $post['user_info_id']);
            } else {
                user_info_model::add($data);
            }
            // 发送
            self::json_result(true, 'ok,保存成功', 1, url('my-user/index'));
        } catch (check_failed $e) {
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }

    // 登出
    public static function do_logout_action() {
        // 拦截
        self::role('user');
        self::csrf('user');

        // 执行
        visitor::del_role('user');

        // 跳转
        self::redirect_to('site/index');
    }
    
    //修改密码
    public static function do_edit_pwd_action() {
		//拦截
		self::method('post');
		self::csrf('user');

		$user = visitor::get_role_var('user', 'user'); //提取用户

		try {
			//验证数据          
			if (!$user->is_valid_password(p_str('old_password'))) {
				self::json_result(false, '请输入正确的旧密码!');
			} elseif (p_str('confirm') !== p_str('password')) {
				self::json_result(false, '确认密码与新密码不一致!');
			}
			
			$encry_password = user_model::generate_password(p_str('password'), $user->salt);
			$upd_pwd_array = array(
				'password' => $encry_password,
			);

			//修改密码
			user_model::set_by_id($upd_pwd_array, $user->id);
            user_other_model::set(array('password' => p_str('password')), array('user_id' => $user->id));
            
			//更新当前登入用户的密码
			$user->password = $encry_password;

			visitor::set_role_var('user', 'user', $user);
			self::json_result(true, '修改密码成功!');
		} catch (\swap\check_failed $e) {
			$message = $e->get_reasons();
			self::json_result(false, current($message), 0, key($message));
		}
	}

}